package com.jiaquyun.jcps.utils

import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.jiaquyun.jcps.entity.OrderListEntity
import com.jiaquyun.jcps.entity.SocketResponse
import com.lemlin.network.HttpServiceFactory
import com.lemlin.network.HttpServiceResponse
import okhttp3.*
import java.util.*


class HttpWebSocketUtils : WebSocketListener() {

    companion object {
        @Volatile
        private var mHttpWebSocketUtils: HttpWebSocketUtils? = null

        private var mWsUrl: String = "wss://mall.jiaquyun.cn/wss"
//        private var mWsUrl: String = "ws://42.194.137.131:10002"

        fun instance(): HttpWebSocketUtils? {
            if (mHttpWebSocketUtils == null) synchronized(
                HttpWebSocketUtils::class.java
            ) {
                if (mHttpWebSocketUtils == null)
                    mHttpWebSocketUtils =
                        HttpWebSocketUtils()
            }
            return mHttpWebSocketUtils
        }
    }

    val socketLiveData = MutableLiveData<SocketResponse>()

    val socketOrderLiveData = MutableLiveData<HttpServiceResponse<SocketResponse>>()

    private var mWebSocket: WebSocket? = null
    private var mWebSocketListener: WebSocketListener? = null

    init {
        connect()
    }

    override fun onOpen(webSocket: WebSocket, response: Response) {
        super.onOpen(webSocket, response)
        val open: String = String.format(
            Locale.CHINA, ("onOpen:%s"), ("WebSocket 已开启")
        )
        LogUtils.e(open)
    }

    override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
        super.onFailure(webSocket, t, response)
        this.close()
        mWebSocketListener?.onFailure(webSocket, t, response)
        val open: String = String.format(
            Locale.CHINA, ("onFailure:%s"), ("WebSocket 异常:${t.message}")
        )
        LogUtils.e(open)
    }

    override fun onMessage(webSocket: WebSocket, text: String) {
        super.onMessage(webSocket, text)
        val response: SocketResponse = try {
            GsonUtils.fromJson<SocketResponse>(text, SocketResponse::class.java)
        } catch (e: Exception) {
            return
        }
        socketLiveData.postValue(response)

        val open: String = String.format(
            Locale.CHINA, ("onMessage:%s"), ("WebSocket 成功:${GsonUtils.toJson(response)}")
        )
        LogUtils.e(open)
    }

    /**
     * 连接
     * */
    private fun connect(): WebSocket? {
        if (mWebSocket == null) mWebSocket =
            HttpServiceFactory.create(
                listener = this,
                wsUrl = mWsUrl
            )
        return mWebSocket
    }

    /**
     * 发送数据
     * */
    fun send(text: String) {
        if (mWebSocket == null)
            connect()
        mWebSocket?.send(text = text)
    }

    /**
     * 取消连接
     * */
    fun cancel() {
        mWebSocket?.cancel()
    }

    /**
     * 关闭连接
     * */
    private fun close() {
        mWebSocket?.close(code = 1000, reason = null)
        mWebSocket = null
    }

//    fun setLocationListener(mWebSocketListener: WebSocketListener) {
//        if (this.mWebSocketListener == null)
//            this.mWebSocketListener = mWebSocketListener
//    }
//
//    interface WebSocketListener {
//        fun onMessage(webSocket: WebSocket, text: String)
//
//        fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?)
//    }
}